#枚举类型
from enum import Enum,unique

WeekDay=Enum('WeekDay',('Sun','Mon','Tue','Wen','Thu','Fri','Sat'))

print(WeekDay)

for name, member in WeekDay.__members__.items():
    print(name, '=>', member, ',', member.value)


#其中的value会自动赋值

#自定义其值
@unique #装饰器 保证其值唯一
class MWeekDay(Enum):#继承枚举类
    Sun=0
    Mon=1
    Tue=2
    Wen=3
    Thu=4
    Fri=5
    Sat=6

#访问属性

#根据key访问
print(MWeekDay.Sat)
print(MWeekDay.Sat.value)
print(MWeekDay['Sat'])

#根据value访问
print(MWeekDay(1))

# 练习

# 把Student的gender属性改造为枚举类型，以避免使用字符串

class Student(object):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

@unique
class Gender(Enum):
    Male = 0
    Female = 1

student1=Student('huangxiaoli',Gender.Male)
student2=Student('gouyd',Gender.Female)

print(student1.gender)
print(student2.gender)